layui.use(['form'], function () {
    var form = layui.form,
        layer = layui.layer;

    function decodeEncodedRandomStringByRsa(randomStringEncodeByDecodeRsaPublicKeyWithBase64 , rsaPrivateKey , password) {
        var encrypt = new JSEncrypt();
        encrypt.setPrivateKey(rsaPrivateKey)
        var randomStringDecodeByDecodeRsaPublicKeyWithBase64 = window.atob(randomStringEncodeByDecodeRsaPublicKeyWithBase64).toString();
        var originRandomString = encrypt.decrypt(randomStringDecodeByDecodeRsaPublicKeyWithBase64);
        var iv  = CryptoJS.enc.Utf8.parse("0123456789ABCDEF");
        var password = password;
        var aesEncrypt = CryptoJS.AES.encrypt(originRandomString+password,   CryptoJS.enc.Utf8.parse(originRandomString),{
            iv:iv,
            mode:CryptoJS.mode.CBC,
            padding:CryptoJS.pad.Pkcs7
        });

        return aesEncrypt.toString();
    }

    function getEncodeRandomStringAndPasswordByAes(randomStringEncodeByDecodeRsaPublicKeyWithBase64 , rsaPrivateKey ,  password) {
        //获取加密字符串
        //加密密码
        //修改data.field的password字段值
        //注册
        console.log(randomStringEncodeByDecodeRsaPublicKeyWithBase64)
        console.log(rsaPrivateKey)
        var encodeRandomStringAndPasswordByAes =  decodeEncodedRandomStringByRsa(randomStringEncodeByDecodeRsaPublicKeyWithBase64 , rsaPrivateKey , password);
        return encodeRandomStringAndPasswordByAes;
    }

    //注册
    form.on('submit(register)', function (data) {
        console.log(data)
        console.log(data.field)
        PostData("/customLoginBusiness/getEncodedRandomString" , {})
            .then((res) => {
                var encodeRandomStringAndPasswordByAes = getEncodeRandomStringAndPasswordByAes(res.data.randomStringEncodeByDecodeRsaPublicKeyWithBase64 , res.data.rsaPrivateKey , data.field.password)
                data.field.password = encodeRandomStringAndPasswordByAes
                console.log(data.field.password)
                var parm = {
                    name : data.field.username,
                    password: data.field.password
                }
                return PostData("/customLoginBusiness/doRegister" , parm)
            }).then((res) => {
        })
            .catch((err) => {
            console.log(err)
        })
        return false;
    })
});
